<?php

namespace app\api\controller;

use app\common\model\Banner;
use app\common\model\Helpers;
use think\Db;
use app\common\model\Users;

class Index extends Base
{


    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        phpinfo();exit;
        $ip = request()->ip();
        echo 'api:';
        echo  $ip;exit('endTest');
    }



    /**
     * 获取banner及广告图
     */
    public function getBanner()
    {
        $model = new Banner();
        $where['status'] = 1;
        $where['cid'] = input('post.cid');
        $limit = is_numeric(input('post.num')) ? input('post.num') : 6;
        $rows = $model->getBanner($where,$limit);
        if (!empty($rows)) api_return(1, '获取成功', $rows);
        api_return(0, '暂无数据');
    }





    /**
     * 发送短信
     */
    public function sms()
    {
        if (request()->isPost()){
            $phone = input('post.phone');
            if (empty($phone)) api_return(0,'手机号码不能为空');
            $has = cache('code_num'.$phone);
            $endToday = strtotime(date('Y-m-d 23:59:59'));
            $time = $endToday - time();
            if ($has){
                if ($has['num'] >= 15)api_return(0,'短信发送过多');
                if ($has['time']+60 > time())api_return(0,'短信发送过于频繁,请一分钟后重试');
            }else{
                cache('code_num'.$phone,['num'=>0,'time'=>time()],$time);
            }
            $result = sendSms($phone);
            if ($result){
                cache('code_num'.$phone,['num'=>$has['num']+1,'time'=>time()],$time);
                api_return(1,'发送成功');
            }else{
                api_return(0,'发送失败');
            }
        }
        api_return(666,999);
    }


    public function test1()
    {
        $data['val1'] = '6398';
        $data['val2'] = '刺激战场';
        var_dump(sendCustomSms(18181366398,$data,'order'));
    }


    //生成微信分享二维码

    public function shareQrcode(){
        $data = request()->param('url');
        $res = code($data);
        api_return(1,'成功',$res);
    }



    /**
     * 版本控制
     */
    public function version()
    {
        $new = Db::name('version')->field('versionCode,versionName,force,url,detail')->order('id desc')->cache(300)->find();
        if (!empty($new)) api_return(1,'获取成功',$new);
        api_return(0,'系统错误');
    }





    /**
     * 获取扩展
     */
    public function explain()
    {
        $field = input('post.field');
        if (empty($field)) api_return(0,'参数不能为空');
        $rows = self::getExplain($field,true);

        api_return(1,'获取成功',$rows);
    }


    /**
     * 获取融云token
     */
    public function token()
    {
        $token = $this->R_token($this->role_id);
        if ($token !== false) api_return(1,'获取成功',$token);
        api_return(0,'服务器繁忙,请稍后重试');
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 广场
     */
    public function square()
    {

        $data = input('post.');

        $user_id = self::getUserId();

        $map = [];

        if ($data['sex']){
            $map['a.sex'] = $data['sex'];
        }else{
            if ($user_id){
                $map['a.sex'] = $this->getSex($user_id);
            }
        }

        if ($data['section']){

            $time = ageDate($data['section']);
            $map['a.birthday'] = ['between time',$time];

        }

        if ($data['skill_id']){
            $map['s.skill_id'] = $data['skill_id'];
            $map['s.is_use'] = 1;
            $map['s.status']  = 1;
        }

        $model = new Users();

        $rows  = $model->rows($map,$user_id);

        api_return(1,'获取成功',$rows);

    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 分类获取
     */
    public function cate(int $type)
    {

        $model = new \app\common\model\Skill();

        $map['type'] = $type;

        $rows = $model->getRows($map);

        api_return(1,'获取成功',$rows);

    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 分类详情
     */
    public function detail(int $id)
    {

        $map['skill_id'] = $id;
//        $map['status']   = 1;

        $model = new \app\common\model\Skill();

        $data  = $model->getDetail($map);

        api_return(1,'获取成功',$data);

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取帮助文章
     */
    public function helpers()
    {
        $model = new Helpers();
        $rows  = $model->getRows(['status'=>1]);
        api_return(1,'获取成功',$rows);
    }




    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 融云在线状态改变
     */
    public function online_status()
    {

        $data = input();

        $appSecret = config('rongyun.appSecret');
        $nonce = $data['nonce'];
        $timestamp = $data['timestamp'];

        $signature = sha1($appSecret.$nonce.$timestamp);

        if ($signature == $data['signature']){
            $item = $data[0];
            $user_id = dehashid($item['userid']);
            if (!is_numeric($user_id))  exit('params error');
            $update['online_time'] = time();
            if ($item['status'] == 1){
                $update['online_status'] = 1;
            }else{
                $room_id = self::staticExtra('room_id',$user_id);
                if ($room_id){
                    $refresh = self::outRoomFunc($user_id,$room_id);
                    if ($refresh){
                        //需要刷新麦序
                        $this->refreshWheat($room_id);
                    }
                }
                $update['room_id'] = 0;
            }
            self::updateUserExtend($user_id,$update);
            exit('success');
        }else{
            api_return(0,'参数错误');
        }
    }

    /**
     * 官网宣传页
     */
    public function web()
    {
        $rows = self::getExplain('companyName,url,IPC,companyAddress,ios,andriod,webKeywords,webDescription,webTitle,phone');
        $this->assign([
            'rows' => $rows,
        ]);
        return view();
    }

    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 礼物列表
     */
    public function giftList()
    {
        $model = new \app\common\model\Gift();

        $map['status'] = 1;

        $rows = $model->giftList($map);

        api_return(1,'获取成功',$rows);

    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 帮助文章
     */
    public function helper()
    {
        $id = input('id');
        $detail = Db::name('helpers')->where('h_id',$id)->cache(15)->find();
        $this->assign([
            'detail' => $detail,
        ]);
        return view();
    }



}
